package com.nbpt;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.nbpt.db.UserDBhelp;

public class SmartCity2 extends AppCompatActivity implements View.OnClickListener{

    private EditText zh;
    private EditText mm;
    private Button btnlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_city);

        zh = findViewById(R.id.zhanghao);
        mm = findViewById(R.id.mima);
        btnlogin = findViewById(R.id.btn);
        btnlogin.setOnClickListener(this);

//        zh.setText("请输入账号");
    }

//    public void loginClick(View view)
//    {
//        String zhanghao = zh.getText().toString();
//        String mima = mm.getText().toString();
//        Log.e("Main","用户名为"+zhanghao);
//        Log.d("Main","密码为"+mima);
//        if (zhanghao.equals("admin") ) {
//            if (mima.equals("123456")) {
//                Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
//                Intent intent = new Intent(SmartCity.this,denglu.class);
//                startActivity(intent);
//            }
//            else if (mima.equals(""))
//                {
//                    Toast.makeText(this, "没有输入密码", Toast.LENGTH_LONG).show();
//                }
//                else
//                    {
//                        Toast.makeText(this, "没有输入密码", Toast.LENGTH_LONG).show();
//                    }
//            }
//            else if (zhanghao.equals(""))
//                {
//                    Toast.makeText(this, "没有输入账号", Toast.LENGTH_LONG).show();
//                }
//                else
//                    {
//                        Toast.makeText(this, "账号输入错误", Toast.LENGTH_LONG).show();
//                    }
//        }


    public void zhuceclick(View view){
        Intent intent = new Intent(SmartCity2.this,RegisterAcitvity.class);
        startActivity(intent);
    }


    @Override
    public void onClick(View view) {
        String zhanghao = zh.getText().toString();
        String mima = mm.getText().toString();
        String tempuser ,temppassword;

        if ((!zhanghao.isEmpty())&&(!mima.isEmpty()))
        {
            UserDBhelp helper = new UserDBhelp(SmartCity2.this,UserDBhelp.VERSION_1);
            Cursor cursor = helper.select(null,null);
            cursor.moveToFirst();
            boolean isRegister =false;                               //标记
            for (int i =0; i<cursor.getCount();i++)
            {
                tempuser = cursor.getString(1);
                temppassword =cursor.getString(2);
                if(!tempuser.equals(zhanghao))
                {
                    //Toast.makeText(SmartCity.this, "账号未注册", Toast.LENGTH_LONG).show();
                    //break;
                }
                else if(tempuser.equals(zhanghao)&&!temppassword.equals(mima))
                {
                    isRegister = true;
                    Toast.makeText(SmartCity2.this, "密码错误", Toast.LENGTH_LONG).show();
                    break;
                }
                if (tempuser.equals(zhanghao)&&temppassword.equals(mima))
                {
                    Toast.makeText(SmartCity2.this,"登陆成功",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(SmartCity2.this,denglu.class);
                    startActivity(intent);
                    isRegister = true;
                    break;
                }
                else
                {
                    cursor.moveToNext();
                }
            }
            if (!isRegister)
            {
                Toast.makeText(SmartCity2.this, "账号未注册", Toast.LENGTH_LONG).show();
            }
//            if (cursor.getCount() != 0)
//            {
//                cursor.moveToFirst();
//                do
//                    {
//                        if (zhanghao.equals(helper.getUsername(cursor))&& mima.equals(helper.getPassword(cursor)))
//                        {
//                            if (zhanghao.equals(helper.getPassword(cursor))){
//                                Toast.makeText(SmartCity.this,"登陆成功",Toast.LENGTH_LONG).show();
//                                }
//                            else{
//                                Toast.makeText(SmartCity.this,"登陆失败",Toast.LENGTH_LONG).show();
//                                }break;
//                        }
//                        else if (zhanghao.equals(helper.getUsername(cursor))&& !mima.equals(helper.getPassword(cursor))) {
//                            Toast.makeText(SmartCity.this, "密码错误", Toast.LENGTH_LONG).show();
//
//                        }
//                        else if (!zhanghao.equals(helper.getUsername(cursor ))) {
//                            Toast.makeText(SmartCity.this, "账号未注册", Toast.LENGTH_LONG).show();
//
//                        }
//                    }
//                    while (cursor.moveToNext());
//                    cursor.close();
//            }
        }


//        Log.e("Main","用户名为"+zhanghao);
//        Log.d("Main","密码为"+mima);
//        if (zhanghao.equals("admin") ) {
//            if (mima.equals("123456")) {
//                Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
//                Intent intent = new Intent(SmartCity.this,denglu.class);
//                startActivity(intent);
//            }
//            else if (mima.equals(""))
//            {
//                Toast.makeText(this, "没有输入密码", Toast.LENGTH_LONG).show();
//            }
//            else
//            {
//                Toast.makeText(this, "没有输入密码", Toast.LENGTH_LONG).show();
//            }
//        }
//        else if (zhanghao.equals(""))
//        {
//            Toast.makeText(this, "没有输入账号", Toast.LENGTH_LONG).show();
//        }
//        else
//        {
//            Toast.makeText(this, "账号输入错误", Toast.LENGTH_LONG).show();
//        }
    }
}
